import Cookies from 'js-cookie';
import { setLanguage } from '@/lib/auth';
import { getLocale } from '@/lib/lang';

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  branchId: {}, // 分道馆id
  device: 'desktop',
  language: getLocale(),
  title: '', // 标题
  markTelephone: '', // 记录手机号码
  settlementGoods: [], // 记录结算的商品
  buyGoods: {}, // 立即购买的商品
  isgetBadge: false, // 是否获取侧边徽标数据
  // 备案号
  archival: '2020068156',
  // 备案号地址
  archivalUrl: 'https://beian.miit.gov.cn',
  // 版权所有
  copyright: '吉金无锡体育装备科技有限公司',
  // 版权所有地址
  copyrightUrl: ''
};

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1);
    } else {
      Cookies.set('sidebarStatus', 0);
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device;
  },
  // 设置标题
  SET_TITLE: (state, title) => {
    state.title = title;
  },
  // 设置语言
  SET_LANGUAGE (state, language) {
    state.language = language;
    setLanguage(language);
  },
  SET_MARKTELEPHONE (state, markTelephone) {
    state.markTelephone = markTelephone;
  },
  SET_SETTLEMENTGOODS (state, settlementGoods) {
    state.settlementGoods = settlementGoods;
  },
  // 立即购买的商品
  SET_BUYGOODS (state, buyGoods) {
    state.buyGoods = buyGoods;
  },
  // 修改是否获取过侧边徽标
  SET_ISGETBADGE (state, isgetBadge) {
    state.isgetBadge = isgetBadge;
  },
  // 修改分道馆id
  SET_ISGETBRANCHID (state, isgetBranchId) {
    state.branchId = isgetBranchId;
  }
};

const actions = {
  toggleSideBar ({ commit }) {
    commit('TOGGLE_SIDEBAR');
  },
  closeSideBar ({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation);
  },
  toggleDevice ({ commit }, device) {
    commit('TOGGLE_DEVICE', device);
  },
  // 设置标题
  setTitle ({ commit }, title) {
    commit('SET_TITLE', title);
  },
  // 设置语言
  setLanguage ({ commit }, language) {
    commit('SET_LANGUAGE', language);
  },
  // 设置记录已输入过的手机号码
  setMarkTelephone ({ commit }, markTelephone) {
    commit('SET_MARKTELEPHONE', markTelephone);
  },
  // 设置记录结算的商品
  setSettlementGoods ({ commit }, settlementGoods) {
    commit('SET_SETTLEMENTGOODS', settlementGoods);
  },
  // 立即购买的商品
  setBuyGoods ({ commit }, buyGoods) {
    commit('SET_BUYGOODS', buyGoods);
  },
  // 修改是否获取过侧边徽标
  setIsGetBadge ({ commit }, isgetBadge) {
    commit('SET_ISGETBADGE', isgetBadge);
  },
  // 修改分道馆id
  setISGetBranchId ({ commit }, isgetBranchId) {
    commit('SET_ISGETBRANCHID', isgetBranchId);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
